package com.yand.oratransmysql.service.impl;

import com.yand.oratransmysql.constant.Constant;
import com.yand.oratransmysql.entity.OracleTableInfoBean;
import com.yand.oratransmysql.mapper.OracleTableInfoMapper;
import com.yand.oratransmysql.service.oracleTableService;
import com.yand.oratransmysql.thread.OracleTransMysqlThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.*;

/**
 * oracle表的处理业务类
 */
@Service
public class OracleTableServiceImpl implements oracleTableService {

    @Autowired
    private OracleTableInfoMapper oracleTableInfoMapper;

    /**
     * 线程池对象
     */
    private ExecutorService threadPool = null;

    /**
     * 定时器
     */
    private CountDownLatch countDownLatch = null;
    /**
     * oracle表的处理逻辑：
     *  1，遍历该库中的所有表,
     *  2，传递表名称查询这个表的所有列与字段类型。
     *  3，查询这个表的所有值，
     *  4，根据查询出来的列与字段类型，写好mysql对应的insert 语句
     *  5，根据一个表一个文件的形式写到特定的文件夹下
     */
    @Override
    public void oracleTableHandle() {

        List<OracleTableInfoBean> oracleTableInfoBeanList = oracleTableInfoMapper.queryAllTableName();

        countDownLatch = new CountDownLatch(oracleTableInfoBeanList.size());

        /**
         * 自定义线程池的实现
         */
        threadPool = new ThreadPoolExecutor(Constant.CORETHREADNUM, Integer.MAX_VALUE,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(Constant.BLOCKQUEUELENGTH));

        for(OracleTableInfoBean oracleTableInfoBean : oracleTableInfoBeanList){


            threadPool.execute(new OracleTransMysqlThread(oracleTableInfoMapper, countDownLatch, oracleTableInfoBean.getTableName()));
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        threadPool.shutdown();
        System.out.println("完成");

    }
}
